//
//  PropoverProvViewController.m
//  evidenceGather
//
//  Created by 王继伟 on 13-9-9.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "PropoverProvViewController.h"
#import "DefTableViewCell.h"
#import "MDProvince.h"
#import "ProviceVO.h"

@interface PropoverProvViewController ()

@end

@implementation PropoverProvViewController

@synthesize loginUser;
@synthesize mdProvince;
@synthesize provKeyValue;
@synthesize provVOs;


-(id) initViewWithCode:(NSString *)userCode{
    
    [super viewDidLoad];
    self.title = @"省份选择";
    
    if(mdProvince==nil){
        mdProvince=[[MDProvince alloc] init];
    }
    NSMutableDictionary *provinceDict=[mdProvince qryProvinceByPkUser:userCode];
    if(provinceDict!=nil){
        provKeyValue=[[NSMutableDictionary alloc] init];
        provVOs =[[NSMutableArray alloc] init];
        NSArray *provinceVOs=[provinceDict allValues];
        NSMutableArray *provMutiArr=[[NSMutableArray alloc] init];
        
        for(int i=0;i<[provinceDict count];i++){
            ProviceVO *provinceVO = [provinceVOs objectAtIndex:i];
            [provVOs addObject:provinceVO];
            [provMutiArr addObject:provinceVO.prvname]; 
            [provKeyValue setObject:[provinceVO prvcode] forKey:[provinceVO prvname]];
        }
        provinceVOs = [provinceVOs sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            ProviceVO *vo1 = (ProviceVO*)obj1;
            ProviceVO *vo2 = (ProviceVO*)obj2;
            if ([vo1.prvcode intValue] > [vo2.prvcode intValue]) {
                return NSOrderedDescending;
            }
            
            if ([vo1.prvcode intValue] < [vo2.prvcode intValue]) {
                return NSOrderedAscending;
            }
            return NSOrderedSame;
        }];
    }
    
    return self;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(provKeyValue!=nil){
        return [provKeyValue count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int index=indexPath.row;
    ProviceVO *provVO=[provVOs objectAtIndex:index];
    
    NSString *cellText = provVO.prvcode;
    cellText =[cellText stringByAppendingString:@"_"];
    cellText =[cellText stringByAppendingString:provVO.prvname];
    DefTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellText];
    cell.cellCode=provVO.prvcode;
    cell.cellName=provVO.prvname;
    
    if(cell == nil)
    {
        cell = [[[DefTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellText] autorelease];
    }
    cell.textLabel.text = cellText;
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProviceVO *provVO=[provVOs objectAtIndex:indexPath.row];
    [self.delegate afterCellSelected:provVO.prvcode cellName:provVO.prvname tag:1];
}
@end
